﻿using UnityEngine;
using UnityEngine.EventSystems;

public class CanvasModeBehaviour : UIBehaviour, ICanvasShowHideMode
{
    Canvas m_BlurUI;
    Canvas m_RootCanvas;
    Transform m_BlurUITrans;
    Transform m_RootCanvasTrans;

    public Canvas BlurUICanvas
    {
        get
        {
            return m_BlurUI;
        }
    }

    public Transform BlurUICanvasTransform
    {
        get
        {
            return m_BlurUITrans;
        }
    }

    public Canvas RootCanvas
    {
        get
        {
            if (m_RootCanvas == null)
            {
                m_RootCanvas = GetComponentInParent<Canvas>();
            }
            return m_RootCanvas;
        }
    }

    public Transform RootCanvasTransform
    {
        get
        {
            if (m_RootCanvasTrans == null && RootCanvas != null)
            {
                m_RootCanvasTrans = RootCanvas.transform;
            }
            return m_RootCanvasTrans;
        }
    }

    public bool CheckIsShow()
    {
        bool isShow = true;
        if (BlurUICanvas == null || RootCanvas == null) return isShow;
        //Debuger.Log("{0}:{1}", RootCanvasTransform.name, RootCanvasTransform.name);
        if (RootCanvasTransform.parent != null && RootCanvasTransform.parent.parent != null && 
            RootCanvasTransform.parent.parent == BlurUICanvasTransform)
        {
            isShow = RootCanvas.enabled && BlurUICanvas.enabled;
        }
        else
        {
            isShow = RootCanvas.enabled;
        }
        return isShow;
    }

    protected override void Awake()
    {
        base.Awake();
        var blur = GameObject.Find("BlurUI");
        if (blur != null)
        {
            m_BlurUI = blur.GetComponent<Canvas>();
            m_BlurUITrans = blur.transform;
        }
    }
}
